<div class="directory" id="directories">
	{% if rtval == "5100" %}
		<div class = "hero-unit">
			<h2>This repository is sick. </h2>
			<p>Ebi is unable to connect to the repository you've requested. Ask the admin to check username and password.</p>
		</div>
	{% else %}
	{% for entry in rtval %}
	{% if entry.kind == 'dir' %}
	<a class="next_level_trigger" onclick="showLoading()" href="/svn/?repo={{ entry.hashed_path }}&&name={{ entry.name }}&&id={{ entry.id }}">
	{% else %}
	<a class="next_level_trigger" onclick="showLoading()" href="/svn/file/?path={{ entry.hashed_path }}&&name={{ entry.name }}&&id={{ entry.id }}">
	{% endif %}
	<div class="container-fluid">
		<div class="row-fluid show-grid">
			<div class="span12 show-grid">
				{% if entry.kind == 'dir' %}
					Directory: {{ entry.name }}
				{% else %}
					File: {{ entry.name }}
				{% endif %}
					<div class="row-fluid show-grid">
						<div class="span9">Last Updated: {{ entry.commit.date }}</div>
						<div class="span3">Latest Revision: {{ entry.commit.revision }}</div>
						
					</div>
			</div>
		</div>
	</div>
	</a>
	{% empty %}
		<div class = "hero-unit">
			<h2>This folder is a bit void. </h2>
			<p>The folder you have requested does not contain any files or folders. If you believe this is a mistake, well, it's not.</p>
		</div>
	{% endfor %}
		{% if user.is_superuser %}
			<div id = "new_element">
				<a class="btn" id="new_element_trigger" onclick="addNewElementToPage()" href="#" style="position: relative; left: 40%">Create New File/Folder</a>
			</div>
		{% endif %}
	{% endif %}

</div>



<script>
{% if user.is_superuser %}

function removeContainer(){
	$('#new_element_container').remove();
	$('#new_element_fluid_container').remove();
	$('#new_element_trigger').show()
}
function ajaxAddNewElement(){
	var getRequestVars = getUrlVars();
	$.ajax({
		type: "POST",
		url: "/svn/createElement/",
		data: {
			repoid: getRequestVars["id"],
			path: getRequestVars["repo"],
			curr: getRequestVars["name"],
			name: document.getElementById('new_element_name').value,
			type: document.getElementById('selected_type').innerHTML
		},
		success: function(msg){
			window.location.reload();
		}
	});
}

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

function addNewElementToPage(){
	$('#new_element_trigger').hide()
	var container = document.createElement('div');
	container.setAttribute('class', 'container-fluid');
	container.setAttribute('style', 'display: none');
	container.setAttribute('id', 'new_element_fluid_container');
	var outerRow = document.createElement('div');
	outerRow.setAttribute('class', 'row-fluid show-grid');
	outerRow.setAttribute('id', 'new_element_container');
	var outerSpan = document.createElement('div');
	outerSpan.setAttribute('class', 'span12 show-grid');
	outerSpan.innerHTML = "<input id=\"new_element_name\" type=\"text\" placeholder=\"Name\" style=\"margin-top: 10px\">";
	outerSpan.innerHTML = outerSpan.innerHTML + "<script>var element_name_check=new LiveValidation(\"new_element_name\");element_name_check.add(Validate.Exclusion,{within:['cow','pigeon','giraffe'],partialMatch:true})<\/script>";
	outerRow.appendChild(outerSpan);
	
	container.appendChild(outerRow);
	var innerRow = document.createElement('div');
	innerRow.setAttribute('class', 'row-fluid show-grid');
	var innerSpan9 = document.createElement('div');
	innerSpan9.setAttribute('class', 'span9');
	innerSpan9.innerHTML = "<div class=\"btn-group\" id =\"type_select\" style=\"margin-top: 5px; \" data-toggle=\"buttons-radio\"><button type=\"button\" onclick=\"setNoticeFolder()\" rel=\"tooltip\" data-placement = \"bottom\" title=\"The new element will be committed.\" onmouseover=\"$('#select_folder').tooltip('show')\" class=\"btn\" id=\"select_folder\">Folder</button><button type=\"button\" class=\"btn\" id=\"select_file\" onclick=\"setNoticeFile()\" rel=\"tooltip\" data-placement = \"bottom\" title=\"The new element will be committed.\" onmouseover=\"$('#select_file').tooltip('show')\">File</button></div>";
	var innerSpan3 = document.createElement('div');
	innerSpan3.setAttribute('class', 'span3');
	innerSpan3.innerHTML = "<div class=\"btn-group\" style=\"margin-top: 5px\"><input class=\"btn btn-primary\" type=\"submit\" onclick=\"ajaxAddNewElement()\"value=\"Create\"/><button class=\"btn btn-danger\" onclick=\"removeContainer()\">Cancel</button></div>";
	innerRow.appendChild(innerSpan9);
	innerRow.appendChild(innerSpan3);

	outerSpan.appendChild(innerRow);

	var selected_type = document.createElement('p');
	selected_type.setAttribute('id', 'selected_type');
	selected_type.setAttribute('style', 'display: none');
	outerRow.appendChild(selected_type);

	var directories = document.getElementById('directories');
	directories.appendChild(container);
	$("#new_element_fluid_container").show('normal');
}

function setNoticeFolder(){
	document.getElementById('selected_type').innerHTML = "Folder";
}

function setNoticeFile(){
	document.getElementById('selected_type').innerHTML = "File";
}
// using jQuery
function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}
var csrftoken = getCookie('csrftoken');
function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
function sameOrigin(url) {
    // test that a given url is a same-origin URL
    // url could be relative or scheme relative or absolute
    var host = document.location.host; // host + port
    var protocol = document.location.protocol;
    var sr_origin = '//' + host;
    var origin = protocol + sr_origin;
    // Allow absolute or scheme relative URLs to same origin
    return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
        (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
        // or any other URL that isn't scheme relative or absolute i.e relative.
        !(/^(\/\/|http:|https:).*/.test(url));
}
$.ajaxSetup({
    beforeSend: function(xhr, settings) {
        if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {
            // Send the token to same-origin, relative URLs only.
            // Send the token only if the method warrants CSRF protection
            // Using the CSRFToken value acquired earlier
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        }
    }
});
{% endif %}
</script>